package com.achobeta.recruitment.service.impl.interact;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.config.AppConfig;
import com.achobeta.recruitment.dao.interact.PageConfigMapper;
import com.achobeta.recruitment.model.PageConfig;
import com.achobeta.recruitment.service.interact.PageConfigService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.MediaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-31
 * Time: 12:48
 */
@Service
@Slf4j
public class PageConfigServiceImpl implements PageConfigService {

    @Value("${redirect.domain}")
    private String redirectDomain;

    @Autowired
    private PageConfigMapper pageConfigMapper;

    @Override
    public PageConfig selectReal() {
        // 调用dao层，查询id为1的配置
        PageConfig pageConfig = pageConfigMapper.selectByPrimaryKey(1);
        // 校验
        CheckUtils.checkNULL(pageConfig);
        // 返回
        log.info(ResultCode.SUCCESS + " real配置信息: " + pageConfig);
        return pageConfig;
    }

    @Override
    public PageConfig selectVirtual() {
        // 调用dao层，查询id为2的配置
        PageConfig pageConfig = pageConfigMapper.selectByPrimaryKey(2);
        // 校验
        CheckUtils.checkNULL(pageConfig);
        // 返回
        log.info(ResultCode.SUCCESS + " virtual配置信息: " + pageConfig);
        return pageConfig;
    }

    @Override
    public PageConfig uploadHomePhoto(MultipartFile file, String path) {
        // 校验
        CheckUtils.checkNULL(file, path);
        long size = MediaUtils.loadFile(file, AppConfig.ROOT + path);
        log.info(ResultCode.SUCCESS + "保存成功：" + redirectDomain + "/" + path);
        // 构造修改对象
        PageConfig updateConfig = new PageConfig();
        updateConfig.setId(2);
        updateConfig.setUpdateTime(new Date());
        updateConfig.setHomePhoto(path);
        updateConfig.setHomePhotoSize((int) (size / 1024));
        // 修改配置表
        int rows = pageConfigMapper.updateByPrimaryKeySelective(updateConfig);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_PAGE_CONFIG_UPDATE);
        log.info(ResultCode.SUCCESS + "  首页配置成功！");
        return updateConfig;
    }

    @Override
    public PageConfig uploadDeliverPhoto(MultipartFile file, String path) {
        // 校验
        CheckUtils.checkNULL(file, path);
        long size = MediaUtils.loadFile(file, AppConfig.ROOT + path);
        log.info(ResultCode.SUCCESS + "保存成功：" + redirectDomain + "/" + path);
        // 构造修改对象
        PageConfig updateConfig = new PageConfig();
        updateConfig.setId(2);
        updateConfig.setUpdateTime(new Date());
        updateConfig.setDeliverPhoto(path);
        updateConfig.setDeliverPhotoSize((int) (size / 1024));
        // 修改配置表
        int rows = pageConfigMapper.updateByPrimaryKeySelective(updateConfig);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_PAGE_CONFIG_UPDATE);
        log.info(ResultCode.SUCCESS + "  投递页配置成功！");
        return updateConfig;
    }

    @Override
    public PageConfig uploadModel(MultipartFile file, String path) {
        // 校验
        CheckUtils.checkNULL(file, path);
        MediaUtils.loadFile(file, AppConfig.ROOT + path);
        log.info(ResultCode.SUCCESS + "保存成功：" + redirectDomain + "/" + path);
        // 构造修改对象
        PageConfig updateConfig = new PageConfig();
        updateConfig.setId(2);
        updateConfig.setUpdateTime(new Date());
        updateConfig.setModelName(path);
        // 修改配置表
        int rows = pageConfigMapper.updateByPrimaryKeySelective(updateConfig);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_PAGE_CONFIG_UPDATE);
        log.info(ResultCode.SUCCESS + "  简历模板配置成功！");
        return updateConfig;
    }

    @Override
    public void saveConfig() {
        // 调用dao层
        int rows = pageConfigMapper.updateRealConfig();
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_PAGE_CONFIG_UPDATE);
        // 成功
        log.info(ResultCode.SUCCESS + "  用户端配置更新！");
    }
}
